Control Statement

제어문
1. 반복문(for, while)
> for(i in 1:3){
+     print(i)
+ }
[1] 1
[1] 2
[1] 3
> data<-c("a", "b", "c")
> for(i in data){
+     print(i)
+ }
[1] "a"
[1] "b"
[1] “c"

> while(i<5){
+     print(i)
+     i<-i+1
+ }
[1] 0
[1] 1
[1] 2
[1] 3
[1] 4
2. 조건문
참과 거짓에 따라 특정 코드가 수행될지 혹은 수행되지 않을지를 결정
> number<-5
> if(number<5){
+     print('number is smaller than 5')
+ }else if(number>5){
+     print('number is bigger than 5')
+ }else{
+     print('number is 5')
+ }
[1] "number is 5”

> number<-3
> if(number<5){
+     print('number is smaller than 5')
+ }else if(number>5){
+     print('number is bigger than 5')
+ }else{
+     print('number is 5')
+ }
[1] "number is smaller than 5"
3. 사용자 정의 함수
> comparedTo5<-function(number){
+     if(number<5){
+         print('number is smaller than 5')
+     }else if(number>5){
+         print('number is bigger than 5')
+     }else{
+         print('number is 5')
+     }
+ }
> comparedTo5(10)
[1] "number is bigger than 5"
> comparedTo5(3)
[1] "number is smaller than 5"
4. 주석
#을 사용하여 주석 표시한다.
> #1+1
> 1+1
[1] 2